package com.lyf.core.action;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lyf.core.compant.Perm;
import com.lyf.core.log.aspect.InfoLog;
import com.lyf.core.log.aspect.OptType;
import com.lyf.core.pojo.compant.SheetData;
import com.lyf.core.pojo.model.PageModel;
import com.lyf.core.service.LogService;
import com.lyf.core.util.CommonUtils;
import com.lyf.core.util.ExcelUtils;

/**
 * 日志 用户操作日志等信息查询 导出
 * @author lyf
 *
 */
@RequestMapping("/logger")
@Controller
@Perm(id="logger_ma" , model="访问日志",parentId="ma" , name="用户日志页面" )
public class Logger {

	@Autowired
	private LogService logService;
	
	@RequestMapping("/logs") 
	@Perm(id="ma_logs" , model="访问日志",parentId="logger_ma" , name="访问日志" )
	public String logs() {
		return null ;
	} 
	
	@RequestMapping("/findLogs") 
	@ResponseBody
	@Perm(id="logger_findLogs" , model="访问日志" ,parentId="ma_logs", name="日志明细查询"  ,isPage=false, optType = OptType.QUERY)
	public PageModel<InfoLog> findLogs(Integer start , Integer end , Integer uid , Integer page, Integer rows){
		if(start == null || end == null) return null ;
		  
		Date s = CommonUtils.parseDate("yyyyMMdd", String.valueOf(start));
		Date e = CommonUtils.parseDate("yyyyMMddHHmmss", String.valueOf(end) + "235959");
		 
		PageModel<InfoLog> model = logService.logPage(s, e, uid, page, rows);
		 
		return model ;
		
	}
	
	
	@RequestMapping(value = "/exportCallInfo" , method=RequestMethod.GET)
	@Perm(id="logger_exportLog" , model="访问日志" ,parentId="ma_logs", name="日志明细导出"  ,isPage=false, optType = OptType.QUERY)
	public void exportCallInfo( Integer start ,   Integer end , Integer uid, HttpServletResponse rep) {
		 
		String filename = ("log") + "_" + start + "_" + end +".xlsx" ;
		
		String model = "call_info.xlsx" ;
		
		List<InfoLog> infos = new LinkedList<InfoLog>();
		
		if(start != null && end != null) {
			 
			Date s = CommonUtils.parseDate("yyyyMMdd", String.valueOf(start));
			Date e = CommonUtils.parseDate("yyyyMMddHHmmss", String.valueOf(end) + "235959");
			
			infos = logService.findDetails(s, e, uid);
		}
		 
		SheetData sd = new SheetData("访问详情");
		sd.addDatas(infos);

		//CommonUtils.writeData("call_info.xlsx", rep, filename, sd);
		ExcelUtils.writeData(model, rep, filename, sd)	 ;
		
	}
	 
	
	
}
